package caseManagerBusiness;

import com.alibaba.fastjson.JSONArray;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by yangxianming on 2018/12/11.
 * 获取用例数据
 *  SELECT `data` FROM testcase WHERE id="6"
 * caseid=xx
 */
public class GetCaseData implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        ReturnJson returnJson = new ReturnJson();
        String caseId = request.getParameter("caseid");
        String where = "id=\"" + caseId + "\"";
        SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
        SqlReturnModel sqlReturnModel = sqlDispatch.select("`data`", "testcase", where, "createtime");
        if (sqlReturnModel.getIsSuccess()){
            String data = sqlReturnModel.getSelectReturnData().get(0).get(0);
            data = data.replaceAll("<yxmsinglequotes>", "'");
//            System.out.println(data);
            returnJson.setResult(JSONArray.parse(data));
        }else {
            returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
        }
        returnJson.setApiStatus((sqlReturnModel.getIsSuccess()) ? 1 : 0);
        return returnJson.toString();
    }
}
